#!/usr/bin/python
#-*- coding: UTF-8 -*-
# Django settings for icq project.

from django.conf import settings
from django.contrib import staticfiles
from django.http import Http404, HttpResponsePermanentRedirect
from django.shortcuts import render_to_response
from django.utils.encoding import *
from icq.traffic_statistics.models import *
import datetime
import time
import string
from icq.traffic_statistics.db import *
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

from django.db import connection, models

#def object_list(request,model):
#obj_list=model.objects.all()
#template_name='templates/traffic_statistics/%s_list.html' % model.__name__.lower()
#return render_to_response(template_name,{'object_list':obj_list})

#整站统计
@login_required
def dashborad(request):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=7)
        endDate = nowtime - datetime.timedelta(days=1)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)

    site = Total()
    o = site.www_total(0, startDate, endDate)
    pv_list = convert_long(o.values_list("pv"))
    uv_list = convert_long(o.values_list("uv"))
    ip_list = convert_long(o.values_list("ip"))
    dt = convert_date(o.values_list("dt"))
    tdt = convert_full_date(o.values_list("dt"))
    aDataSet = []
    count = len(pv_list)

    for i in range(0, count):
        temp = []
        temp.append(tdt[i])
        temp.append(pv_list[i])
        temp.append(uv_list[i])
        temp.append(ip_list[i])
        aDataSet.append(temp)
    aoColumns = [{'sTitle': 'Date'}, {'sTitle': 'PV'}, {'sTitle': 'UV'}, {'sTitle': 'IP'}, ]
    return render_to_response('traffic_statistics/dashboard.html', locals(), context_instance=RequestContext(request))

#频道汇总统计
@login_required
def channel_total(request):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=1)
        endDate = nowtime - datetime.timedelta(days=1)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)
    site = Total()
    o = site.channel_total(startDate, endDate)
    if o:
        pv_list = []
        uv_list = []
        ip_list = []
        dname_list = []
        for v in o:
            pv_list.append(int(v[0]))
            uv_list.append(int(v[1]))
            ip_list.append(int(v[2]))
            dname_list.append(v[3])
        aDataSet = []
        count = len(pv_list)

        for i in range(0, count):
            temp = []
            name = dname_list[i]
            temp.append(name)
            temp.append(pv_list[i])
            temp.append(uv_list[i])
            temp.append(ip_list[i])
            aDataSet.append(temp)
        aoColumns = [{'sTitle': 'Channel'}, {'sTitle': 'PV'}, {'sTitle': 'UV'}, {'sTitle': 'IP'}, ]
        bar_height = count * 80
    return render_to_response('traffic_statistics/channel_total.html', locals(),
                              context_instance=RequestContext(request))

#论坛汇总统计
@login_required
def group_total(request):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=1)
        endDate = nowtime - datetime.timedelta(days=1)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)
    site = Total()
    o = site.group_total(startDate, endDate)
    if o:
        pv_list = []
        uv_list = []
        ip_list = []
        dname_list = []
        for v in o:
            pv_list.append(int(v[0]))
            uv_list.append(int(v[1]))
            ip_list.append(int(v[2]))
            dname_list.append(v[3])
        aDataSet = []
        count = len(pv_list)

        for i in range(0, count):
            temp = []
            name = dname_list[i]
            temp.append(name)
            temp.append(pv_list[i])
            temp.append(uv_list[i])
            temp.append(ip_list[i])
            aDataSet.append(temp)
        aoColumns = [{'sTitle': 'Group'}, {'sTitle': 'PV'}, {'sTitle': 'UV'}, {'sTitle': 'IP'}, ]
        bar_height = count * 80
    return render_to_response('traffic_statistics/channel_total.html', locals(),
                              context_instance=RequestContext(request))


@login_required
#页面访问量Top100
def pv_top100(request):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=1)
        endDate = nowtime - datetime.timedelta(days=0)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)
    site = Total()
    o = site.pv_top100(startDate, endDate)
    if o:
        dname_list = []
        cname_list = []
        pv_list = []
        url_list = []
        for v in o:
            dname_list.append(v[0])
            cname_list.append(v[1])
            pv_list.append(v[2])
            url_list.append(v[3])
        aDataSet = []
        count = len(dname_list)

        for i in range(0, count):
            temp = []
            temp.append(pv_list[i])
            url = url_list[i]
            url = '<a href="' + url + '" target="_blank">' + url + '</a>'
            temp.append(url)
            temp.append(cname_list[i])
            name = dname_list[i]
            temp.append(dname_list[i])
            aDataSet.append(temp)
        aoColumns = [{'sTitle': 'PV'}, {'sTitle': 'URL'}, {'sTitle': 'Channel'}, {'sTitle': 'Department'}, ]
    else:
        message = '没有数据'
    return render_to_response('traffic_statistics/top_list.html', locals(),
                              context_instance=RequestContext(request))


@login_required
#页面贡献流量Top100
def refer_top100(request):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=1)
        endDate = nowtime - datetime.timedelta(days=0)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)
    site = Total()
    o = site.refer_top100(startDate, endDate)
    if o:
        pv_list = []
        url_list = []
        for v in o:
            pv_list.append(v[0])
            url_list.append(v[1])
        aDataSet = []
        count = len(pv_list)

        for i in range(0, count):
            temp = []
            temp.append(pv_list[i])
            url = url_list[i]
            url = '<a href="' + url + '" target="_blank">' + url + '</a>'
            temp.append(url)
            aDataSet.append(temp)
        aoColumns = [{'sTitle': 'PV'}, {'sTitle': 'URL'}, ]
    else:
        message = '没有数据'
    return render_to_response('traffic_statistics/top_list.html', locals(),
                              context_instance=RequestContext(request))


#事业部汇总统计
@login_required
def department_all(request):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=1)
        endDate = nowtime - datetime.timedelta(days=0)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)
    site = Total()
    o = site.department_all_total(startDate, endDate)
    if o:
        pv_list = []
        uv_list = []
        ip_list = []
        dname_list = []
        for v in o:
            pv_list.append(int(v[0]))
            uv_list.append(int(v[1]))
            ip_list.append(int(v[2]))
            dname_list.append(v[3])
        aDataSet = []
        count = len(pv_list)

        for i in range(0, count):
            temp = []
            name = dname_list[i]
            temp.append(name)
            temp.append(pv_list[i])
            temp.append(uv_list[i])
            temp.append(ip_list[i])
            aDataSet.append(temp)
        aoColumns = [{'sTitle': 'Department'}, {'sTitle': 'PV'}, {'sTitle': 'UV'}, {'sTitle': 'IP'}, ]
    else:
        message = '没有数据'
    return render_to_response('traffic_statistics/department_all_total.html', locals(),
                              context_instance=RequestContext(request))

#事业部
@login_required
def department_single(request, did):
    if 'startPicker' in request.GET and request.GET['startPicker']:
        if 'endPicker' in request.GET and request.GET['endPicker']:
            startDate = request.GET['startPicker']
            endDate = request.GET['endPicker']
    else:
        nowtime = datetime.datetime.today()
        startDate = nowtime - datetime.timedelta(days=7)
        endDate = nowtime - datetime.timedelta(days=1)
        startDate = str(startDate.year) + "-" + str(startDate.month) + "-" + str(startDate.day)
        endDate = str(endDate.year) + "-" + str(endDate.month) + "-" + str(endDate.day)
    did = int(did) + 1
    site = Total()
    o = site.department_single_total(did, startDate, endDate)
    pv_list = []
    uv_list = []
    ip_list = []
    dt = []
    tdt = []
    for v in o:
        pv_list.append(int(v[0]))
        uv_list.append(int(v[1]))
        ip_list.append(int(v[2]))
        dt.append((v[3]).day)
        tdt.append(str(v[3].year) + '-' + str(v[3].month) + '-' + str(v[3].day))
    aDataSet = []
    count = len(pv_list)

    for i in range(0, count):
        temp = []
        temp.append(tdt[i])
        temp.append(pv_list[i])
        temp.append(uv_list[i])
        temp.append(ip_list[i])
        aDataSet.append(temp)
    aoColumns = [{'sTitle': 'Date'}, {'sTitle': 'PV'}, {'sTitle': 'UV'}, {'sTitle': 'IP'}, ]
    return render_to_response('traffic_statistics/department_single_total.html', locals(),
                              context_instance=RequestContext(request))





